class_name BallStateCarried
extends BallState


#运球频率和球被带时候被踢的一个冲量
const DRIBBLE_FREQUENCY : float = 10
const DRIBBLE_INTENSITY : float = 3
#运球过程中，球距离运动员的距离偏移量
const OFFSET_FROM_PLAYER : Vector2 = Vector2(10,4)



var dribble_time :=0.0



func _enter_tree() -> void:
	#assert(ball.carrier!=null)
	if ball.carrier==null:
		transtition_state(Ball.State.FREEFORM)
	else:
		ball.height_velocity = 0.0
		ball.height = 0.0
	GameEvents.ball_possessed.emit(carrier.fullname)


func _process(delta: float) -> void:
	
	var vx := 0.0
	if carrier.velocity != Vector2.ZERO:
		dribble_time += delta
		if carrier.velocity.x != 0:
			vx = cos(DRIBBLE_FREQUENCY*dribble_time) * DRIBBLE_INTENSITY
		
		if carrier.heading.x >= 0:
			animation_player.play("roll")
			animation_player.advance(0)
		else:
			animation_player.play_backwards("roll")
			animation_player.advance(0)
	else:
		animation_player.play("idle")
		process_gravity(delta)
		dribble_time = 0.0

	ball.position = carrier.position + Vector2(carrier.heading.x * OFFSET_FROM_PLAYER.x + vx,OFFSET_FROM_PLAYER.y)





func _exit_tree() -> void:
	GameEvents.ball_released.emit()
